'''集体解密为明文,放到备份文件夹Backup.只有密码一样的才能一起解密备份.'''

import sys
import getpass

import StringCrypt

## 数据包,包括密码,要备份的文件列表
data = {'key':None,'files':[]}
## 加密解密器
sc = StringCrypt.StringCrypt()
## 是否显示密码
snShow = False

# 参数检查
if len(sys.argv) == 1:
    input('没有要备份的文件,按回车退出!')
    sys.exit(0)
else:
    data['files'] = sys.argv[1:]
    print('要解密备份文件为:\n')
    for i in range(len(data['files'])):
        print(data['files'][i])

# 输入解密密码,用getpass模块,输入不会显示也不会进退位
x = input('输入 1+回车显示密码,其他+回车不显示密码\n')
if(x == '1'):
    snShow = True
if(snShow):
    data['key'] = input('Input pass word:\n')
else:
    data['key'] = getpass.getpass('Input pass word:\n')
    
for fn in data['files']:
    newFn = sys.path[0]+'\\Backup\\'+fn.split('\\')[-1]+'.bak'
    with open(fn,'rb') as f:
        cryptString = f.read().decode('utf8')
    decryptString = sc.AESDecrypt(cryptString,data['key'])
    with open(newFn,'wb') as f:
        f.write(decryptString.encode('utf8'))
    print('成功备份明文文件:\n'+newFn)
    
# 结束
input('脚本结束,输入回车退出')